
var http = require('http');
var url  = require('url');
var fs = require('fs');
var nowjs = require('now');

var server = http.createServer (function( request, response) {
	var regex = new RegExp("^/Home/Index/?$" );
	var pathname = url.parse ( request.url ).pathname;
	if ( regex.test(pathname) ){
		response.writeHead( 200, { "content-type": "text/html" } ) ;
		fs.readFile( "./index.html", function( err, data ) {
			response.write( data);
			response.end();
		});
	}
	else {
		response.writeHead( 404, { "content-type": "text/plain" } );
		response.end ("File Not found") ;
	}
});

server.listen(8003);
console.log ("SERVER RUNNING http://localhost:8003"); 



//	-------	End server stuff  --------- ///

var nowjs = require("now");
var everyone = nowjs.initialize(server);

// List of available rooms
everyone.now.serverRoomsList = {'room1':'Room 1','room2':'Room 2','room3':'Room 3'};

// Send message to everyone on the users group
everyone.now.distributeMessage = function(message){
    //console.log('Received message from '+this.now.name +' in serverroom '+this.now.serverRoom);
    var group = nowjs.getGroup(this.now.serverRoom);
    group.now.receiveMessage(this.now.name+'@'+this.now.serverRoom, message);
};

everyone.now.changeRoom = function(newRoom){
    var oldRoom = this.now.serverRoom;
    console.log('Changed user '+this.now.name + ' from '+oldRoom + ' to '+newRoom);
    //if old room is not null; then leave the old room
    if(oldRoom){
        var oldGroup = nowjs.getGroup(oldRoom);
        oldGroup.removeUser(this.user.clientId);
        // Tell everyone he left :)
        oldGroup.now.receiveMessage('SERVER@'+oldRoom, this.now.name + ' has left the room and gone to '+newRoom);
    }
    var newGroup = nowjs.getGroup(newRoom);
    newGroup.addUser(this.user.clientId);
    // Tell everyone he joined
    newGroup.now.receiveMessage('SERVER@'+newRoom, this.now.name + ' has joined the room');
    this.now.serverRoom = newRoom;
};
/*
everyone.now.countDown = function(){
	var c=0;
	var t;
	var timer_is_on=0;

	function timedCount()
	{
		document.getElementById('timer').value=c;
		c=c+1;
		t=setTimeout("timedCount()",1000);
	}

	function doTimer()
	{
		if (!timer_is_on)
  		{
 			timer_is_on=1;
  			timedCount();
  		}
	}
};*/


everyone.now.generateStocks = function(stocks){
	
};

